var api = require('../../utils/api.js');
var WxParse = require('../../wxParse/wxParse.js');
import Card from '../../palette/card';
var app = getApp();
Page({
  imagePath: '',
  data: {
    article: '',
    post: false,
    template: {},
    post_excerpt: false,
    qvideo: false,
    nvideo: false,
    taoke:'article.post_taoke',
  },

  onLoad(params) {
    //console.log(options);
    this.params = params;
  },
  onReady() {
    this.loadData();
  },
  onImgOK(e) {
    this.imagePath = e.detail.path;
    this.setData({
      imagePath: this.imagePath
    })
  },
  close() {
    this.setData({
      post: false
    })
  },
  saveImage() {
    wx.saveImageToPhotosAlbum({
      filePath: this.imagePath,
    });
  },
  loadData() {
    var params = this.params;
    if (params.id) {
      wx.showNavigationBarLoading();
      api.get({
        url: 'portal/articles/' + params.id,
        data: {},
        success: data => {
          wx.hideNavigationBarLoading();
          if (data.code) {
            let publishedDate = new Date();
            publishedDate.setTime(data.data.published_time * 1000);
            data.data.published_date = publishedDate.format('yyyy-MM-dd hh:mm');
            this.setData({
              article: data.data
            });
            let img = data.data.thumbnail ? data.data.thumbnail : ''
            console.log(img)
            this.setData({
              template: new Card().palette(data.data.post_title, img, data.data.post_keywords),
            });
          
            WxParse.wxParse('articleContent', 'html', data.data.post_content, this, 30);
            wx.setNavigationBarTitle({
              title: data.data.post_title
            });
          }
        },
        fail: err => {
          wx.hideNavigationBarLoading();
        }
      });
    }


  },
  onFavoriteTap(e) {
    api.post({
      url: 'user/favorites',
      data: {
        object_id: this.params.id,
        table_name: 'portal_post',
        url: JSON.stringify({
          "action": "portal/Article/index",
          "param": {
            "id": this.params.id
          }
        }),
        title: this.data.article.post_title,
        description: this.data.article.post_excerpt,
      },
      success: data => {
        if (data.code) {
          wx.showToast({
            title: '收藏成功!',
            icon: 'success',
            duration: 1000
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => {}
    });

  },
  onLikeTap(e) {
    api.post({
      url: 'portal/articles/doLike',
      data: {
        id: this.params.id
      },
      success: data => {
        if (data.code) {
          wx.showToast({
            title: '点赞成功!',
            icon: 'success',
            duration: 1000
          });
          this.setData({
            'article.post_like': data.data.post_like
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => {}
    });
  },
  onShareTap() {
    wx.showShareMenu({
      withShareTicket: true
    });
  },
  onShare() {
    this.setData({
      post: true
    })
  },
  //弹窗事件
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //优惠券链接复制
  copyText: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  onShareAppMessage() {
    return {
      title: this.data.article.post_title,
      path: '/pages/article/article?id=' + this.params.id
    }
  }

});